<script setup>
import { Document, Setting } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { computed, onMounted, onBeforeUnmount } from 'vue'
import { connectWebSocket } from '@/share/websocket'

const route = useRoute()
let ws = null


// 初始化WebSocket
const initWebSocket = async () => {
    ws = await connectWebSocket(`ws://${window.location.hostname}:3900`, (data) => {
        try {
            const message = typeof data === 'string' ? JSON.parse(data) : data

            // 新增：处理服务端的内容更新
            if (message.type === 'repy-server-ip') {
                console.log('server ipaddress::', message.ip);
            }

        } catch (e) {
            console.error('消息解析错误:', e)
        }
    })
}

// 计算当前活跃菜单项，处理嵌套路由情况
const activeMenu = computed(() => {
    // console.log('renderer layout active menu path::', route.path);
    return route.path === '/' ? '/' : '/project-list';
})

// 发送 ws
const sendIpUpdate = () => {
    if (ws) {
        ws.send(JSON.stringify({
            type: 'get-server-ip',
            content: 'get'
        }))
    }
}


onMounted(async () => {
    await initWebSocket()
    setTimeout(() => {
        sendIpUpdate()
    }, 500);

})
onBeforeUnmount(() => ws?.close())
</script>

<template>
    <div class="app-container">
        <!-- 左侧菜单区 (15%) -->
        <div class="left-menu">
            <el-menu router :default-active="activeMenu" text-color="#000" active-text-color="#409EFF">
                <el-menu-item index="/">
                    <Document />
                    <span>系统信息</span>
                </el-menu-item>
                <el-menu-item index="/project-list">
                    <Setting />
                    <span>活动管理</span>
                </el-menu-item>
            </el-menu>
        </div>

        <!-- 右侧内容区 (85%) -->
        <div class="right-content">
            <div class="content-header">
                <h3>{{ $route.meta.title || '内容区域' }}</h3>
            </div>
            <router-view />
        </div>
    </div>
</template>
<style scoped>
/* 原有布局样式保持不变 */
.app-container {
    display: flex;
    /* height: 100vh; */
    max-height: calc(100vh - 10px);
}

.right-content {
    width: 82%;
}

.left-menu {
    width: 18%;
}

.el-menu-item {
    display: flex;
    align-items: center;
    font-size: 18px;
    font-weight: 500;
    gap: 9px;
    white-space: nowrap;
    margin-bottom: 20px;
}

.el-menu-item:first-child {
    margin-top: 80px;
}

.el-menu-vertical {
    height: 100%;
    border-right: none;
}

svg {
    width: 20px !important;
    height: 20px !important;
    vertical-align: middle;
}

.content-header {
    margin-bottom: 30px;
}

.content-body {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-menu-item:first-child {
    margin-top: 80px;
}

.el-menu-vertical {
    height: 100%;
    border-right: none;
}

svg {
    width: 20px !important;
    height: 20px !important;
    vertical-align: middle;
}

.layout-action-bar {
    position: fixed;
    left: 18%;
    right: 0;
    bottom: 0;
    z-index: 999;
    width: 82%;
}

.right-content {
    width: 82%;
    padding-right: 0;
}

.content-header {
    margin-bottom: 10px;
    margin-left: 2em;
}

.content-body {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.bottom-action-bar {
    position: fixed;
    width: 78%;
    right: 0;
    bottom: 0;
    z-index: 999;
    padding: 16px 20px;
    background: white;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.1);
}
</style>
